import React from 'react';
import {inject, observer} from 'mobx-react';
import {toJS} from "mobx";
import {Button, Col, Modal, Radio, Row, Tooltip} from 'antd';
import {
    WeaBrowser,
    WeaCheckbox,
    WeaDialog,
    WeaFormItem,
    WeaInputNumber,
    WeaLocaleProvider,
    WeaRightMenu,
    WeaSearchGroup,
    WeaSelect,
    WeaTab,
    WeaInput
} from 'ecCom';
import {WeaTableNew} from "comsMobx";
import AccessAgentSave from './AccessAgentSave'
import * as Util from '../../../util/Util'

const RadioGroup = Radio.Group;
const RadioButton = Radio.Button;
const WeaTable = WeaTableNew.WeaTable;
const getLabel = WeaLocaleProvider.getLabel;

/**
 * 绩效-考核代理
 */
@inject("accessAgentStore")
@observer
export default class AccessAgent extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            selectedKey: '0',
            show: false,//显示代理列表
            showSave: false,//显示代理保存
            data: {},
            isAdd: false,//是否新增操作
        }
        this.scrollHeight = {y: Util.getCurrentHeight() * 0.5};
        this.store = this.props.accessAgentStore;
    }

    render() {
        const {tableStore, doAgentData, getAgentListData, username, userid, listParams, onListParamsChange, showSearchAd} = this.store;
        const {show, selectedKey, showSave, data, isAdd} = this.state;
        return (
            <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@n3dxrf`}
                icon='icon-coms-implement'
                iconBgcolor='rgb(150, 53, 138)'
                style={{width: 1024, height: 600}}
                title={getLabel('519370', '考核代理')}
                visible={show}
                onCancel={() => this.doShow(false)}
                buttons={[<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@vpo8zm@close`} type="ghost" onClick={() => this.doShow(false)}>关闭</Button>]}
                destroyBodyOnClose={true}
            >
                <WeaTab ecId={`${this && this.props && this.props.ecId || ''}_WeaTab@u48t02`}
                    datas={this.getTab()}
                    advanceHeight={200}
                    keyParam="viewcondition"
                    selectedKey={selectedKey}
                    buttons={this.getButton(this.props.ecId || '')}
                    searchType={["advanced"]}
                    showSearchAd={showSearchAd}
                    searchsAd={this.getSearchs()}
                    hideSearchAd={() => this.store.showSearchAd = false}
                    setShowSearchAd={bool => {
                        this.store.showSearchAd = bool
                    }}
                    onChange={v => {
                        this.setState({selectedKey: v});
                        if (v == 0) {
                            onListParamsChange({
                                myagent: true,
                                agenterIds: "",
                                agenterNames: ""
                            });
                        } else {
                            onListParamsChange({
                                myagent: false,
                                beagenterIds: "",
                                beagenterNames: ""
                            });
                        }
                        getAgentListData();
                    }}
                    buttonsAd={this.getTabButtonsAd(this.props.ecId || '')}
                />
                <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@ul90wi`} datas={this.getRightButton()} style={{height: '100%'}} showUrlItem={false}>
                    <WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@r1wgj1`}
                        comsWeaTableStore={tableStore}
                        hasOrder
                        needScroll
                        scroll={this.scrollHeight}
                        onOperatesClick={this.onOperatesClick}
                    />
                    {showSave && <AccessAgentSave ecId={`${this && this.props && this.props.ecId || ''}_AccessAgentSave@luhtzz`}
                        userid={userid}
                        username={username}
                        data={isAdd ? {isscore: '1', programtype: '1,2,3,4'} : data}
                        beagenterData={[{id: userid, name: username}]}
                        isAdd={isAdd}
                        show={showSave}
                        save={(params) => {
                            doAgentData(params);
                            this.setState({showSave: false})
                        }}
                        onClose={() => this.setState({showSave: false})}
                    />}
                </WeaRightMenu>
            </WeaDialog>
        )
    }

    //获取操作按钮
    getTabButtonsAd(ecId) {
        const {resetSearchAd, advanceSearch} = this.store;
        return [
            (<Button ecId={`${ecId}_Button@kiw3ex@agentsearch`} type="primary" onClick={advanceSearch}>{getLabel(388113, '搜索')}</Button>),
            (<Button ecId={`${ecId}_Button@8ajnnd@agentreset`} type="ghost" onClick={resetSearchAd}>{getLabel(2022, '重置')}</Button>),
            (<Button ecId={`${ecId}_Button@k5yen8@agentcancel`} type="ghost" onClick={() => this.store.showSearchAd = false}>{getLabel(31129, '取消')}</Button>)
        ]
    }

    //获取高级搜索条件
    getSearchs() {
        const {onListParamsChange, listParams, beagentData, agenterData} = this.store;
        const statusData = [
            {key: "", showname: getLabel('332', '全部')},
            {key: "0", showname: getLabel('520808', '未结束')},
            {key: "1", showname: getLabel(383462, '未开始')},
            {key: "2", showname: getLabel('33348', '代理中')},
            {key: "3", showname: getLabel('383417', '已结束')},
        ];
        const programtypeData = [
            {key: "0", showname: getLabel('332', '全部')},
            {key: "1", showname: getLabel('19398', '月度')},
            {key: "2", showname: getLabel('17495', '季度')},
            {key: "3", showname: getLabel('20729', '半年')},
            {key: "4", showname: getLabel('17138', '年度')},
        ];

        let items = [];
        !listParams.myagent && items.push({
            com:
                <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@okcuhr@principal`} label={getLabel('17565', "被代理人")} ratio1to2>
                    <WeaBrowser ecId={`${this && this.props && this.props.ecId || ''}_WeaBrowser@nfapxx@principal`}
                        type={17}
                        isSingle={false}
                        title={getLabel(179, '人力资源')}
                        replaceDatas={beagentData}
                        onChange={(ids, names) => {
                            onListParamsChange({beagenterIds: ids, beagenterNames: names});
                        }}
                    />
                </WeaFormItem>
        });
        listParams.myagent && items.push({
            com:
                <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@05kzkh@hrmid`} label={getLabel('17566', "代理人")} ratio1to2>
                    <WeaBrowser ecId={`${this && this.props && this.props.ecId || ''}_WeaBrowser@zo9pby@hrmid`}
                        type={17}
                        isSingle={false}
                        title={getLabel(179, '人力资源')}
                        replaceDatas={agenterData}
                        onChange={(ids, names) => {
                            onListParamsChange({agenterIds: ids, agenterNames: names});
                        }}
                    />
                </WeaFormItem>
        });
        items.push({
            com:
                <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@3yi9xr@agentstatus`} label={getLabel('33362', '代理状态')} ratio1to2>
                    <WeaSelect ecId={`${this && this.props && this.props.ecId || ''}_WeaSelect@ybq4cw@agentstatus`}
                        value={listParams.status}
                        options={statusData}
                        onChange={(val) => onListParamsChange({status: val})}
                    />
                </WeaFormItem>
        });
        items.push({
            com:
                <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@50c9ng@agenttype`} label={getLabel('520809', '代理考核类型')} ratio1to2>
                    <WeaSelect ecId={`${this && this.props && this.props.ecId || ''}_WeaSelect@a2yo4s@agenttype`}
                        value={listParams.programtype}
                        options={programtypeData}
                        onChange={(val) => onListParamsChange({programtype: val})}
                    />
                </WeaFormItem>
        });
        return <WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@58yytm`} title={getLabel(32905, '常用条件')} items={items} showGroup={true} col={2}/>;
    }

    //获取右键操作按钮
    getRightButton = () => {
        const {selectedKey} = this.state;
        const {getAgentListData} = this.store;
        let menu = [];
        if (selectedKey == 0) {
            menu.push({
                key: '0', icon: <i className="icon-coms-Add-to"/>, content: getLabel(82, '新建'),
                onClick: () => this.setState({showSave: true, isAdd: true})
            });
        }
        menu.push({
            key: '1', icon: <i className='icon-coms-search'/>, content: getLabel(197, '搜索'),
            onClick: getAgentListData
        })
        return menu;
    }

    //获取tab
    getTab = () => {
        return [{
            title: getLabel('383596', '我的代理设置'),
            viewcondition: "0"
        }, {
            title: getLabel('519371', '我代处理的评分'),
            viewcondition: "1"
        }]
    }

    //表格每行右侧菜单操作
    onOperatesClick=(record, index, operate, flag)=> {
        const {doAgentData, getAgentListData} = this.store;
        if (operate.index === '0') {
            this.setState({
                data: {
                    ...record,
                    beginDate: record.iseditstartdate == 0 ? "" : record.beginDate,
                    beginTime: record.iseditstarttime == 0 ? "" : record.beginTime,
                    endDate: record.iseditenddate == 0 ? "" : record.endDate,
                    endTime: record.iseditendtime == 0 ? "" : record.endTime,
                }, showSave: true, isAdd: false
            })
        } else if (operate.index === '1') {
            Modal.confirm({
                title: getLabel('386786', '信息确认'),
                content: getLabel('520807', '确定要收回代理吗？'),
                onOk: () => {
                    //收回代理
                    doAgentData({
                        operation: 'withdrawAgent',
                        id: record.id
                    });
                    getAgentListData();
                },
            });
        }
    }

    //获取操作按钮
    getButton = (ecId) => {
        const {selectedKey} = this.state;
        if (selectedKey == 0) {
            return [
                <Button ecId={`${ecId}_Button@8pdp5g@newagent`}
                    style={{marginRight: '10px',lineHeight:'16px'}}
                    type="primary"
                    onClick={() => this.setState({showSave: true, isAdd: true})}
                >
                    {getLabel(82, '新建')}
                </Button>
            ];
        }
        return [];
    }

    //更改显示状态
    doShow = (val) => {
        if (val) {
            this.store.getAgentListData();
        }
        this.setState({show: val});
    }

};
